package com.xsherl.leetcode.solution;

public class AddBinary {

    public String addBinary(String a, String b) {
        int i = a.length() - 1, j = b.length() - 1;
        int c = 0;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 || j >= 0){
            int x = i >= 0 ? a.charAt(i) - '0' : 0;
            int y = j >= 0 ? b.charAt(j) - '0' : 0;
            int m = x + y + c;
            if (m >= 2){
                m -= 2;
                c = 1;
            } else {
                c = 0;
            }
            sb.append(m);
            i--;
            j--;
        }
        if (c == 1){
            sb.append(1);
        }
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        String s = "1010";
        String b = "1010";
        String s1 = new AddBinary().addBinary(s, b);
        System.out.println(s1);
    }

}
